//
//  SYDetailsViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/25.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit
import SnapKit

class SYDetailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SYStatusCellDelegate{
 
    
    var status: SYStatus? {
        didSet {
        self.detailsTableView.reloadData()
        }
    }
    
    var comments: [SYComments]? {
        didSet{
            self.detailsTableView.reloadData()
        }
    }
    
    func loadComments() {
        
        SYComments.loadStatusCommentsModel(status!.id) { (comments, error) -> () in
            if error != nil {
                return
            }
            
            let count = comments?.count ?? 0
                
            if count == 0 {
                return
            }
            print("conut\(count)")
            self.comments = comments
            
        }
    }

    
    override func viewDidLoad() {
        prepareUI()
        loadComments()
    }
    
    //MARK: - 准备UI
    func prepareUI() {
        
        
        self.view.addSubview(detailsTableView)
        detailsTableView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.view.snp_top)
            make.left.equalTo(self.view.snp_left)
            make.right.equalTo(self.view.snp_right)
            make.bottom.equalTo(self.view.snp_bottom).offset(-49)
        }
        self.view.addSubview(bottomView)
        bottomView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom)
            make.left.equalTo(self.view.snp_left)
            make.right.equalTo(self.view.snp_right)
            make.height.equalTo(44)
        }

    }
    
    //MARK: - 数据源方法
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else {
            return self.comments?.count ?? 0
        
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let status = self.status
        if indexPath.section == 0 {
            
            let cellID = status!.cellID()
            let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! SYStatusCell
            cell.delegate = self
            cell.status = status
            return cell
        } else {
            
            let comment = self.comments![indexPath.row]
            let cell = tableView.dequeueReusableCellWithIdentifier("cellReuseIdentifier") as! SYDetailCell
            cell.comments = comment
            return cell
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            let status = self.status!
            if status.rowHeight != nil {
                return status.rowHeight!
            } else {
                let cellID = status.cellID()
                let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! SYStatusCell
            
                let rowHeight = cell.rowHeiht(status)
                status.rowHeight = rowHeight
                return rowHeight
            }
        } else {
            let comments = self.comments![indexPath.row]
            if comments.rowHeight != nil {
                return comments.rowHeight!
            } else {
                
                let cell = tableView.dequeueReusableCellWithIdentifier("cellReuseIdentifier") as! SYDetailCell
                let rowHeight = cell.rowHeiht(comments)
                comments.rowHeight = rowHeight
                
                return rowHeight
            }
        }
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        if section == 1 {
            let headerString = "评论"
            headerString
            
            return "评论"
        }
        return nil
    }
    
    //MARK: - 懒加载
    lazy var detailsTableView: UITableView = {
        let tableView = UITableView()
        
        tableView.registerClass(SYStatusNormalCell.self, forCellReuseIdentifier: SYStatusCellReuseIdentifier.SYStatusNormalCell.rawValue)
        tableView.registerClass(SYStatusForwardCell.self, forCellReuseIdentifier: SYStatusCellReuseIdentifier.SYStatusForwardCell.rawValue)
        tableView.registerClass(SYDetailCell.self, forCellReuseIdentifier: "cellReuseIdentifier")
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var bottomView: SYStatusBottomView = SYStatusBottomView()
 
    //MARK: - 代理方法
    func forwradOnTheClick(status: SYStatus) {
        let detailVC = SYDetailsViewController()
       detailVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(detailVC, animated: true)
     
        detailVC.status = status
        
    }
    

}
